package ModelLayer;
import java.util.ArrayList;

public class ProductContainer {
	private ArrayList<Product> productContainer;
	private static ProductContainer instance;
	private Product pObject; 

	public ProductContainer()
	{
		productContainer = new ArrayList<Product>();
	}

	public static ProductContainer getInstance()
	{
		if(instance == null)
		{
			instance = new ProductContainer();
		}
		return instance;
	}

	public ArrayList<Product> getProducts()
	{
		return productContainer;
	}

	public Product findProductByName(String name)
	{
		pObject = null;
		int index = 0;
		boolean found = false;
		while(index < productContainer.size() && !found)
		{ 
			if(productContainer.get(index).getName().equals(name))
			{
				pObject = productContainer.get(index);
				found = true;
			}
			else
			{
				index++;
			}
		}//end while
			return pObject;
	}

	public boolean addProduct(Product p)
	{
		boolean added = false;
		if(findProductByName(p.getName()) == null)
				{
			productContainer.add(p);
			added = true;
				}
		return added;
	}

	public boolean deleteProductByBarCode(String name)
	{
		if(productContainer.contains(findProductByName(name)))
		{
			productContainer.remove(findProductByName(name));
			return true;
		}
		else
		{
			return false;
		}
	}
}


